//
//  KYMVProgressView.swift
//  Test
//
//  Created by zhaopeng on 2020/6/3.
//  Copyright © 2020 zhaopeng. All rights reserved.
//
import UIKit


@objc protocol KYMVProgressSliderDelegate:NSObjectProtocol {
    /// isHiddenSliderView = True  触发
    /// - Parameter progressView:
    func progressSliderShow(progressView:KYMVProgressSliderView)
    /// isHiddenSliderView = false 触发
    /// - Parameter progressView:
    func progressSliderTouchDown(progressView:KYMVProgressSliderView)
    /// 开始拖动
    /// - Parameter progressView:
    func progressSliderBegan(progressView:KYMVProgressSliderView)
    /// 正在拖动中
    /// - Parameter progressView:
    @objc optional func progressSliderChanged(progressView:KYMVProgressSliderView)
    /// 多动结束
    /// - Parameter progressView:
    func progressSliderEnded(progressView:KYMVProgressSliderView)
}

class KYMVProgressSliderView: UIButton {
    
    open var progress:Float = 0.0 { // if set,  default 0.0.
        didSet {
            updateProgress()
        }
    }
    
    open var value:Float = 0.0 // default 0.0. this value will be pinned to min/max
    
    open var minimumValue:Float = 0.0 //default 0.0. the current value may change if outside new min value
    
    open var maximumValue:Float = 1.0  // default 1.0. the current value may change if outside new max value
    
    open var isContinuous:Bool = true // if set, value change events are generated any time the value changes due to dragging. default = YES
    
    open var isHiddenSliderView:Bool = true // if set, hidden thumbView,minimumValueView,maximumValueView. default = YES
        {
        didSet{
            setHiddenSliderView(isHiddenSliderView: isHiddenSliderView)
        }
    }
    
    
    weak var delegate:KYMVProgressSliderDelegate?//delegate  KYMVProgressDelegate
    
    /// 结束后是否播放 根据之前播放器的状态来定义
    @objc public var isSliderEndPlay:Bool = false
    
   open lazy var progressView:UIProgressView = {
        let progress = UIProgressView(frame: CGRect.zero)
        progress.trackImage = UIImage()
        progress.progressTintColor = UIColor.magenta
        return progress
    }()

    open lazy var thumbView: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "MV_slider_thumb_image"), for: .normal)//根据自己情况修改按钮图标
        btn.setImage(UIImage(named: "MV_slider_thumb_image"), for: .highlighted)//根据自己情况修改按钮图标
        return btn
    }()
    
   open lazy var minimumValueView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
   open lazy var maximumValueView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.red
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       setUI()
          setHiddenSliderView(isHiddenSliderView: isHiddenSliderView)
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showSlider(sender:)))
                gestureRecognizer.numberOfTapsRequired = 1
        self.addGestureRecognizer(gestureRecognizer)
        
    }
    
    func setUI() {
        progressView.frame = CGRect(x: 0, y: (self.bounds.size.height - 2) / 2.0, width: self.bounds.size.width, height: 1)
        addSubview(progressView)
        
        addSubview(minimumValueView)
        addSubview(maximumValueView)
        addSubview(thumbView)
        thumbView.frame = CGRect(x: 0, y: 0, width: self.bounds.size.height, height: self.bounds.size.height)
      updateValueFrame()
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(pan(sender:)))
        thumbView.addGestureRecognizer(pan)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置隐藏显示滑条控件
    /// - Parameter isHiddenSliderView: Bool
    func setHiddenSliderView(isHiddenSliderView:Bool) {
        thumbView.isHidden = isHiddenSliderView
        minimumValueView.isHidden = isHiddenSliderView
        maximumValueView.isHidden = isHiddenSliderView
    }
    
    
    @objc func showSlider(sender:UIPanGestureRecognizer) {
        
        if isHiddenSliderView {
         isHiddenSliderView = false
            delegate?.progressSliderShow(progressView: self)
            return
        }
        let point = sender.location(in: self)
        setThumbViewX(x: point.x)
        
        delegate?.progressSliderTouchDown(progressView: self)
    }
    @objc func pan(sender:UIPanGestureRecognizer) {

        let point = sender.location(in: self)
        setThumbViewX(x: point.x)
        switch sender.state {
        case .began:
            delegate?.progressSliderBegan(progressView: self)
        case .changed:
            if isContinuous {
                if ((delegate?.responds(to: #selector(delegate?.progressSliderChanged(progressView:)))) == true ){
                      delegate?.progressSliderChanged!(progressView: self)
                }
            }
        case .ended:
            delegate?.progressSliderEnded(progressView: self)
        default:
            break
        }
        
    }
    
    func setThumbViewX(x:CGFloat) {
        
        var thumbView_x = x
        
        let view_width = self.bounds.size.width
        
        let max_x = view_width + thumbView.bounds.size.width / 2.0
        
        //判断左右边距
        if thumbView_x < 0 {
            thumbView_x = 0
        }
        if thumbView_x > max_x {
            thumbView_x = max_x
        }

        let x = thumbView_x - thumbView.bounds.size.width / 2.0
        
        thumbView.frame = CGRect(x: x, y: 0, width: thumbView.bounds.size.width, height: thumbView.bounds.size.height)
        
         let progress =  thumbView_x / self.bounds.size.width
        progressView.progress = Float(progress)
         updateValueFrame()

    }
    
    func updateProgress() {
        if !progress.isNaN {
            progressView.setProgress(progress, animated: false)
            setThumbViewX(x: self.bounds.size.width * CGFloat(progress))
        }
        
    }
    
    func updateValueFrame() {
        let view_width = self.bounds.size.width
        
        let min_w = thumbView.center.x
        
        minimumValueView.frame = CGRect(x: 0, y: (self.bounds.size.height - 2) / 2.0, width: min_w, height: 2)
        maximumValueView.frame = CGRect(x: min_w, y: minimumValueView.frame.origin.y, width: view_width - min_w, height: 2)
        value = maximumValue * progressView.progress
        
    }
    
}
